<?php
	include_once("inc/conn.php");
    include_once("inc/function.php");
    
    if(!isset($_SESSION['usersid'])) {
		echo "您还没登录或者链接超时，请先去<a href='/login.php'>登录</a>!";
		exit;
	}
	$act = intval($_GET['act']);
	//返回界面
	GetGameTrendContent($act);
	
    /* 取得界面
    * 
    */
    function GetGameTrendContent($act)
    {
		$sid = intval($_GET['sid']);
		$numRec = ($_GET['num'] == "")?100:intval($_GET['num']);
		if($numRec > 500)
			$numRec = 500;
		$RetContent  = "<link rel='stylesheet' type='text/css' href='style/trend.css' />";
		$RetContent .= "<div class='trend'>\r\n";
		$RetContent .= "\t<div class='Content'>\r\n";
		//取得子菜单
		//$RetContent .= GetSubMenu($act,$sid);
		
		$RetContent .= "\t</div>\r\n"; //content结束
		//取号码表格
		$RetContent .= GetTableContent($act,$numRec);
		
		$RetContent .= "</div>\r\n";
		//取css
		$RetContent .= "";
		//js 定义
		$RetContent .= GetJSContent($act,$sid,$numRec);
		echo $RetContent;
		exit;
    }
    
    /* 取号码表格
    *
    */
    function GetTableContent($act,$numRec)
    {
    	global $db;
    	$tablegame = GetGameTableName($act,"game");
    	$divWidthStyle = " style='width:100%;' ";
    	$TableWidthStyle = " style='border-collapse:collapse;width:100%;' ";   
		//标准次数
		if($act == "0" || $act == "3" || $act == "4" || $act == "8" || $act == "18")
		{
			$arrStdTimes = array(0,0,1,1,2,2,3,4,5,6,6,7,7,8,8,7,7,6,6,5,4,3,2,2,1,1,0,0);
			$rewardNumCnt = 28;
			$reward_num_type = 'game28';
			$step = 0;
			$divWidthStyle = " style='width:1200px;' ";
    		$TableWidthStyle = " style='border-collapse:collapse;width:1200px;' ";   
		}
    	else if($act == "1" || $act == "5" || $act == "9" || $act == "19")
    	{
    		 $arrStdTimes = array(0,1,3,5,7,10,12,12,12,12,10,7,5,3,1,0); 
    		 $rewardNumCnt = 16; 
    		 $reward_num_type = 'game16';
    		 $step = 3;
		}
    	else if($act == "2" || $act == "10" || $act == "20")
    	{
    		 $arrStdTimes = array(2,5,8,11,14,16,14,11,8,5,2);
    		 $rewardNumCnt = 11;
    		 $reward_num_type = 'game11';
    		 $step = 2;
		}   
    	else if($act == "6" || $act == "7" || $act == "15"|| $act == "23"|| $act == "24")
    	{
    		$arrStdTimes = array(10,10,10,10,10,10,10,10,10,10);
    		$rewardNumCnt = 10;
    		$reward_num_type = 'game10';
    		$step = 1;
		}
		else if ($act == "14")	// 还没弄
		{
			$arrStdTimes = array(0,0,1,1,2,2,3,4,5,6,6,6,6,5,4,3,2,2,1,1,0,0);
			$rewardNumCnt = 22;
			$reward_num_type = 'game22';
			$step = 4;
		}
		else if ($act == "11" || $act == "12" || $act == "13" || $act == "21")	// 还没弄
		{
			$arrStdTimes = array(2,3,5,3,2);
			$rewardNumCnt = 5;
			$reward_num_type = 'game36';
			$step = 1;
		}
		else if ($act == "16")	// 还没弄
		{
			$arrStdTimes = array(5,5);
			$rewardNumCnt = 2;
			$reward_num_type = 'gamelh';
			$step = 1;
		}
		else if ($act == "17")
		{
			$arrStdTimes = array(0,1,3,5,7,10,12,12,13,12,12,10,7,5,3,1,0);
			$rewardNumCnt = 17;
			$reward_num_type = 'gamegyj';
			$step = 3;
		}

		//实际次数
		$arrCurTimes = array();
		$arrCurTimes[0] = "实际次数";
		for($i = 0; $i < $rewardNumCnt; $i++)
		{
			$arrCurTimes[$i] = 0;
		}
		// $arrCurTimes[$rewardNumCnt+7] = "尾数";
		// $arrCurTimes[$rewardNumCnt+8] = "余数"; 
		
		//表格主体
		$arrTH = array();
		$arrTD = array();
    	$arrTH[0] = "<th width='50'>期号</th>";
    	$arrTH[1] = "<th width='60'>时间</th>";	 
    	$arrTD[0] = "";
    	$arrTD[1] = "";
    	$sql = "SELECT GROUP_CONCAT(num SEPARATOR '|') AS strnum FROM gameodds WHERE game_type = '{$reward_num_type}' ORDER BY num";
    	$result = $db->query($sql);
    	if($rs = $db->fetch_array($result))
    	{
    		$arrStdNums = explode("|",$rs['strnum']);
    	}
    	else
    	{
			return "无法取得标准赔率";
    	}
    	for($i = 0; $i < $rewardNumCnt; $i++)
    	{
			$arrTH[$i+2] = "<th width='22'>" . $arrStdNums[$i] . "</th>";
			$num = $rewardNumCnt / 4;
			if($rewardNumCnt == 10 || $rewardNumCnt == 7)
			{
				if($i < $num || $i >= 3 * $num - 1)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else if($rewardNumCnt == 16)
			{
				if($i <= $num || $i >= 3 * $num - 1)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else if($rewardNumCnt == 11)
			{
				if($i <= $num || $i > 2 * $num + 2)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else if($rewardNumCnt == 22)
			{
				if($i <= $num + 1 || $i >= 3 * $num - 2)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else if ($rewardNumCnt == 17)		//
			{
				if($i <= $num + 1 || $i >= 3 * $num - 2)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else if ($rewardNumCnt == 5)
			{
				if($i <= $num + 1 || $i >= 3 * $num - 2)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else if ($rewardNumCnt == 2)
			{
				if($i <= $num + 1 || $i >= 3 * $num - 2)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
			else
			{
				if($i < $num + 3 || $i > 3 * $num - 4)
					$arrTD[$i+2] = "<td class='bgnum'></td>";
				else
					$arrTD[$i+2] = "<td></td>";
			}
    	}
    	// $arrTH[$rewardNumCnt+2] = "<th width='22'>单</th>";
    	// $arrTH[$rewardNumCnt+3] = "<th width='22'>双</th>";
    	// $arrTH[$rewardNumCnt+4] = "<th width='22'>中</th>";
    	// $arrTH[$rewardNumCnt+5] = "<th width='22'>边</th>";
    	// $arrTH[$rewardNumCnt+6] = "<th width='22'>大</th>";
    	// $arrTH[$rewardNumCnt+7] = "<th width='22'>小</th>";
    	// $arrTH[$rewardNumCnt+8] = "<th width='22'>大</th>";
    	// $arrTH[$rewardNumCnt+9] = "<th width='22'>小</th>";
    	// $arrTH[$rewardNumCnt+10] = "<th width='22'>3/</th>";
    	// $arrTH[$rewardNumCnt+11] = "<th width='22'>4/</th>";
    	// $arrTH[$rewardNumCnt+12] = "<th width='22'>5/</th>";
    	
    	// $arrTD[$rewardNumCnt+2] = "<td>单</td>";
    	// $arrTD[$rewardNumCnt+3] = "<td>双</td>";
    	// $arrTD[$rewardNumCnt+4] = "<td>中</td>";
    	// $arrTD[$rewardNumCnt+5] = "<td>边</td>";
    	// $arrTD[$rewardNumCnt+6] = "<td>大</td>";
    	// $arrTD[$rewardNumCnt+7] = "<td>小</td>";
    	// $arrTD[$rewardNumCnt+8] = "<td>大</td>";
    	// $arrTD[$rewardNumCnt+9] = "<td>小</td>";
    	// $arrTD[$rewardNumCnt+10] = "<td></td>";
    	// $arrTD[$rewardNumCnt+11] = "<td></td>";
    	// $arrTD[$rewardNumCnt+12] = "<td></td>";
    	
    	//取数据
    	$sql = "SELECT id,kgtime,kgjg FROM {$tablegame} WHERE kj = 1 ORDER BY kgtime DESC LIMIT {$numRec}";
    	$result = $db->query($sql);
    	//WriteLog(implode("|",$arrTD));
    	$secondTableBody = "";
    	while($rs = $db->fetch_array($result))
    	{ 
    		
			$arrTmpTD = $arrTD;
			$arrTmpTD[0] = "<td class='tdbg3'>" . $rs['id'] . "</td>";
			$arrTmpTD[1] = "<td class='black777'>" . date("m-d H:i",strtotime($rs['kgtime'])) . "</td>";
			//中奖号
			$arrkjNum = explode("|",$rs['kgjg']);
			$kjNum = $arrkjNum[count($arrkjNum)-1];
			//写进中奖号
			$num = $rewardNumCnt / 4;
			$index = $kjNum+2-$step;
			$seq = $kjNum - $step;
			if($rewardNumCnt == 10 || $rewardNumCnt == 7)
			{
				$arrCurTimes[$index-1]++;
				if($seq < $num || $seq >= 3 * $num - 1 )
					$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>{$kjNum}</i></em></td>";
				else
					$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>";
			} 
			else if($rewardNumCnt == 16)
			{
				$arrCurTimes[$index-1]++;
				if($seq <= $num || $seq >= 3 * $num - 1 )
					$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>{$kjNum}</i></em></td>";
				else
					$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>"; 
			}
			else if($rewardNumCnt == 11)
			{
				$arrCurTimes[$index-1]++;
				if($seq <= $num || $seq > 2 * $num + 2 )
					$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>{$kjNum}</i></em></td>";
				else
					$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>"; 
			}
			else if($rewardNumCnt == 22)
			{
				$arrCurTimes[$index-1]++;
				if($seq <= $num + 1 || $seq >= 3 * $num - 2 )
					$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>{$kjNum}</i></em></td>";
				else
					$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>"; 
			}
			else if($rewardNumCnt == 17)	//	gyz
			{
				$arrCurTimes[$index-1]++;
				if($seq <= $num + 1 || $seq >= 3 * $num - 2 )
					$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>{$kjNum}</i></em></td>";
				else
					$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>";
			}
			else if($rewardNumCnt == 5)
			{
				$arrCurTimes[$index-1]++;
				if($seq <= $num + 1 || $seq >= 3 * $num - 2 )
					if ($kjNum == 1){
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>豹</i></em></td>";
					}else if ($kjNum == 2){
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>对</i></em></td>";
					}else if ($kjNum == 3){
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>顺</i></em></td>";
					}else if ($kjNum == 4){
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>半</i></em></td>";
					}else if ($kjNum == 5){
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>杂</i></em></td>";
					}

				//else
					//$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>";
			}
			else if ($rewardNumCnt == 2)
			{
				$arrCurTimes[$index-1]++;
				if($seq <= $num + 1 || $seq >= 3 * $num - 2 )
					if ($kjNum == 1){
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>虎</i></em></td>";
					}else{
						$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>龙</i></em></td>";
					}

				//else
					//$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>";
			}
			else
			{
				$arrCurTimes[$index-1]++;
				if($seq < $num + 3 || $seq > 3 * $num - 4 )
					$arrTmpTD[$index] = "<td class='bgnum'><em class='final'><i>{$kjNum}</i></em></td>";
				else
					$arrTmpTD[$index] = "<td><em class='final'><i>{$kjNum}</i></em></td>"; 
			}
			
			//单双。。。等
			/*if($kjNum % 2 != 0)
			{
				$arrTmpTD[$rewardNumCnt+2] = "<td class='bgkai01'>单</td>";
				$arrCurTimes[$rewardNumCnt+1]++;
			}
			else
			{
				$arrTmpTD[$rewardNumCnt+3] = "<td class='bgkai02'>双</td>";
				$arrCurTimes[$rewardNumCnt+2]++;
			}
			//中
			$num = $rewardNumCnt / 3;
			if($rewardNumCnt == 16)
			{  
				if($kjNum-$step >= $num - 1 & $kjNum-$step < 2 * $num)
				{
					$arrTmpTD[$rewardNumCnt+4] = "<td class='bgkai03'>中</td>";
					$arrCurTimes[$rewardNumCnt+3]++;
				}
			}
			else if($rewardNumCnt == 10 || $rewardNumCnt == 7)
			{ 
				if($kjNum-$step >= $num - 1 & $kjNum-$step <= 2 * $num)
				{
					$arrTmpTD[$rewardNumCnt+4] = "<td class='bgkai03'>中</td>";
					$arrCurTimes[$rewardNumCnt+3]++;
				}
			}
			else if($rewardNumCnt == 22)
			{ 
				if($kjNum-$step > $num - 1 && $kjNum-$step < 2 * $num)
				{
					$arrTmpTD[$rewardNumCnt+4] = "<td class='bgkai03'>中</td>";
					$arrCurTimes[$rewardNumCnt+3]++;
				}
			}
			else if($rewardNumCnt == 11)
			{     
				if($kjNum-$step > $num - 1 && $kjNum-$step < 2 * $num)
				{
					$arrTmpTD[$rewardNumCnt+4] = "<td class='bgkai03'>中</td>";
					$arrCurTimes[$rewardNumCnt+3]++;
				}
			}
			else
			{ 
				if($kjNum-$step >= $num && $kjNum-$step < 2 * $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+4] = "<td class='bgkai03'>中</td>";
					$arrCurTimes[$rewardNumCnt+3]++;
				}
			}
			//边
			$num = $rewardNumCnt / 4;
			if($rewardNumCnt == 10 || $rewardNumCnt == 7)
			{
				if($kjNum-$step < $num || $kjNum-$step >= 3 * $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+5] = "<td class='bgkai04'>边</td>";
					$arrCurTimes[$rewardNumCnt+4]++;
				}
			}
			else if($rewardNumCnt == 16)
			{
				if($kjNum-$step <= $num || $kjNum-$step >= 3 * $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+5] = "<td class='bgkai04'>边</td>";
					$arrCurTimes[$rewardNumCnt+4]++;
				}
			}
			else if($rewardNumCnt == 11)
			{  
				if($kjNum-$step <= $num || $kjNum-$step > 2 * $num + 1)
				{
					$arrTmpTD[$rewardNumCnt+5] = "<td class='bgkai04'>边</td>";
					$arrCurTimes[$rewardNumCnt+4]++;
				}
			}
			else if($rewardNumCnt == 22)
			{  
				if($kjNum-$step <= $num + 1 || $kjNum-$step >= 3 * $num - 2)
				{
					$arrTmpTD[$rewardNumCnt+5] = "<td class='bgkai04'>边</td>";
					$arrCurTimes[$rewardNumCnt+4]++;  
				}
			}
			else
			{    
				if($kjNum-$step < $num + 3 || $kjNum-$step > 3 * $num - 4)
				{
					$arrTmpTD[$rewardNumCnt+5] = "<td class='bgkai04'>边</td>";
					$arrCurTimes[$rewardNumCnt+4]++;
				}
			}
			//大
			$num = $rewardNumCnt / 2;
			if($rewardNumCnt == 11)
			{   
				if($kjNum-$step >= $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+6] = "<td class='bgkai05'>大</td>";
					$arrCurTimes[$rewardNumCnt+5]++;
				}
			}
			else if($rewardNumCnt == 7)
			{    
				if($kjNum-$step >= $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+6] = "<td class='bgkai05'>大</td>";
					$arrCurTimes[$rewardNumCnt+5]++;
				}
			}
			else
			{  
				if($kjNum-$step >= $num)
				{
					$arrTmpTD[$rewardNumCnt+6] = "<td class='bgkai05'>大</td>";
					$arrCurTimes[$rewardNumCnt+5]++;
				}
			}
			//小
			$num = $rewardNumCnt / 2;
			if($rewardNumCnt == 11)
			{     
				if($kjNum-$step < $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+7] = "<td class='bgkai06'>小</td>";
					$arrCurTimes[$rewardNumCnt+6]++;
				}
			}
			else if($rewardNumCnt == 7)
			{   
				if($kjNum-$step < $num - 1)
				{
					$arrTmpTD[$rewardNumCnt+7] = "<td class='bgkai06'>小</td>";
					$arrCurTimes[$rewardNumCnt+6]++; 
				}
			}
			else
			{   
				if($kjNum-$step < $num)
				{
					$arrTmpTD[$rewardNumCnt+7] = "<td class='bgkai06'>小</td>";
					$arrCurTimes[$rewardNumCnt+6]++;
				}
			}
			//尾数大
			if($kjNum % 10 >= 5)
				$arrTmpTD[$rewardNumCnt+8] = "<td class='bgkai07'>大</td>";
			//尾数小
			if($kjNum % 10 < 5)
				$arrTmpTD[$rewardNumCnt+9] = "<td class='bgkai08'>小</td>";
			//余数3
			$arrTmpTD[$rewardNumCnt+10] = "<td class='black333'>". ($kjNum % 3) ."</td>";
			//余数4
			$arrTmpTD[$rewardNumCnt+11] = "<td class='black333'>". ($kjNum % 4) ."</td>";
			//余数5
			$arrTmpTD[$rewardNumCnt+12] = "<td class='black333'>". ($kjNum % 5) ."</td>";*/
			//写进表
			$secondTableBody .= "\t\t\t<tr>\r\n";
			$secondTableBody .= implode("\r\n",$arrTmpTD) . "\r\n";
			$secondTableBody .= "\t\t\t</tr>\r\n";
			
    	}
    	
		//表格数据
		//$divTable = "<div class='table table table-striped table-bordered table-hover' {$divWidthStyle}>\r\n";
		//$divTable .= "\t<table class='table_list' cellspacing='0px' {$TableWidthStyle}>\r\n";
		$divTable = "<div class='table'>\r\n";
		$divTable .= "\t<table class='table_list'  cellspacing='0px'>\r\n";
		//第一个tbody
		$divTable .= "\t\t<tbody>\r\n";
		//头
		$divTable .= "\t\t\t<tr bgcolor='#fbfbfb'>\r\n";
		$divTable .= "\t\t\t\t<th colspan='". ($rewardNumCnt+13) ."'>走势图 <select id='sltNum'>\r\n";
		for($i = 1; $i <= 5; $i++)
		{
			$theRecCnt = $i * 100;
			if($theRecCnt == $numRec)
				$divTable .= "\t\t\t\t\t<option value={$theRecCnt} selected='selected'>最新{$theRecCnt}期</option>\r\n";
			else
				$divTable .= "\t\t\t\t\t<option value={$theRecCnt}>最新{$theRecCnt}期</option>\r\n";
		} 
		$divTable .= "\t\t\t\t</select></th>\r\n";
		$divTable .= "\t\t\t</tr>\r\n";
		//标准次数
		$divTable .= "\t\t\t<tr class='timeh'>";
		$divTable .= "\t\t\t\t<th colspan='2'><b class='black777'>标准次数</b></th>";
		foreach($arrStdTimes as $v)
		{
			$divTable .= "\t\t\t\t<th>{$v}</th>\r\n";
		}
		$divTable .= "\t\t\t</tr>\r\n";
		//实际次数
		$divTable .= "\t\t\t<tr class='timeh'>\r\n"; 
		for($i = 0; $i < count($arrCurTimes); $i++)
		{
			if($i == 0 || $i == count($arrCurTimes)-2)
				$divTable .= "\t\t\t<th colspan=1><b class='black777'>{$arrCurTimes[$i]}</b></th>\r\n";
			else if($i == count($arrCurTimes)-1)
				$divTable .= "\t\t\t<th colspan=1><b class='black777'>{$arrCurTimes[$i]}</b></th>\r\n";
			else
				$divTable .= "\t\t\t<th>{$arrCurTimes[$i]}</th>\r\n";
		}
		$divTable .= "\t\t\t</tr>\r\n";
		//标题
		$divTable .= "\t\t\t<tr class='font_color_2' bgcolor='#e3f0ff'>\r\n"; 
		$divTable .= "\t\t\t" . implode("\r\n",$arrTH);
		$divTable .= "\t\t\t</tr>\r\n";
		$divTable .= "\t\t\t</tr>\r\n";
		
		$divTable .= "\t\t</tbody>\r\n";
		//第二个tbody
		$divTable .= "\t\t<tbody>\r\n";
		
		$divTable .= $secondTableBody;
		
		$divTable .= "\t\t</tbody>\r\n";
		$divTable .= "\t</table>";
		$divTable .= "</div>\r\n";
		
		return $divTable;
    }
    
    /* 取得JS
    * 
    */
    function GetJSContent($act,$sid)
    {
		$js = "<script type=\"text/javascript\">";
		$js .= "
			$(document).ready(function(){
				$('#sltNum').change(function(){
					var v = $('#sltNum').val();
					if(v != ''){
						getContent('strend.php?act={$act}&sid={$sid}&num='+ v);
					}
				});
			});  
		";
		
		$js .= "</script>\r\n";
		return $js;
    }
